package com.ihighest.app.infox.activity;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.QuestionSurveyListAdapter;

/**
 * 调查问卷详情界面
 * 
 * @author lkx Created at 2015-10-29 上午11:14:36
 * @version 1.0
 */
public class QuestionSurveyDetailActivity extends BaseActivity implements OnClickListener {
	private String id;
	private ListView questionListview;
	// private ResizeListView questionListview;
	private String commitId;
	private String isJoin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_questionsurvey_detail);
		id = getIntent().getStringExtra("id");
		isJoin = getIntent().getStringExtra("isJoin");
		initView();
	}

	/**
	 * 绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-27 下午4:00:27
	 */
	private void initView() {
		findViewById(R.id.questionSurveyDetailBack).setOnClickListener(this);
		findViewById(R.id.commitSurvey).setOnClickListener(this);
		questionListview = (ListView) findViewById(R.id.questionSurveyDetailListView);
		initData(id);
	}

	private void initData(String questionId) {
		String url = "survey/" + questionId + ".json";
		Api.data(url, new ApiCallback(QuestionSurveyDetailActivity.this) {

			@Override
			public void handleSuccess(ApiResult result) {
				try {
					questionListview.setAdapter(new QuestionSurveyListAdapter(QuestionSurveyDetailActivity.this, result.getData()
							.getJSONArray("q")));
					commitId = result.getData().getString("id");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				// TODO Auto-generated method stub

			}
		}, QuestionSurveyDetailActivity.this);

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.questionSurveyDetailBack:
			finish();
			break;

		case R.id.commitSurvey:
			if (isJoin.equals("no")) {
				if (!TextUtils.isEmpty(SharedPreferencesKit.getString(QuestionSurveyDetailActivity.this, Const.SP_KEY_USER_ID))) {
					ApiParam param = ApiParam.create();
					param.addParam("id", commitId);
					Iterator iter = QuestionSurveyListAdapter.map.entrySet().iterator();
					while (iter.hasNext()) {
						Map.Entry entry = (Map.Entry) iter.next();
						String key = (String) entry.getKey();
						List<String> val = (List<String>) entry.getValue();
						param.addParam(key, listToString(val));
						System.out.println("key" + key + "value" + val.size());
					}
					Api.post("survey/submit", param, new ApiCallback(QuestionSurveyDetailActivity.this) {

						@Override
						public void handleSuccess(ApiResult result) {
							finish();

						}

						@Override
						public void handleFailure(Exception e, String msg) {
							// TODO Auto-generated method stub

						}
					}, QuestionSurveyDetailActivity.this);
				} else {
					ToastKit.showShort(QuestionSurveyDetailActivity.this, Const.UN_LOGIN_ALERT);
				}
			} else {
				ToastKit.showShort(QuestionSurveyDetailActivity.this, "您已参与该项调查");
			}

			break;
		}

	}

	/**
	 * @Description:把list转换为一个用逗号分隔的字符串
	 */
	public static String listToString(List list) {
		StringBuilder sb = new StringBuilder();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				if (i < list.size() - 1) {
					sb.append(list.get(i) + ",");
				} else {
					sb.append(list.get(i));
				}
			}
		}
		return sb.toString();
	}
}
